using System;
using System.Html;
using System.Runtime.CompilerServices;
using jQueryApi;

namespace Nsb.Classes
{
    public partial class DomElement
    {
        public DomElement FromHtml(string html)
        {
            Element = jQuery.FromHtml(html);
            return this;
        }

        [AlternateSignature]
        public static extern Truple GetTruple(Element el);

        public static Truple GetTruple(jQueryObject el)
        {
            if (!(bool)Script.Literal("({0} instanceof jQuery)", el))
                el = jQuery.FromElement((Element)(object)el);
            jQueryObject t = el.Closest(".Truple");
            if (t.Length > 0)
                return (Truple)t.GetDataValue("truple");
            return null;
        }

        [AlternateSignature]
        public extern DomElement Append(jQueryObject el);

        public DomElement Append(DomElement de)
        {
            if ((bool)Script.Literal("({0} instanceof jQuery)", de))
                Element.Append((jQueryObject)(object)de);
            else
                Element.Append(de.Element);
            return this;
        }

        [AlternateSignature]
        public extern DomElement Prepend(jQueryObject el);

        public DomElement Prepend(DomElement de)
        {
            if ((bool)Script.Literal("({0} instanceof jQuery)", de))
                Element.Prepend((jQueryObject)(object)de);
            else
                Element.Prepend(de.Element);
            return this;
        }

        [AlternateSignature]
        public extern DomElement AppendTo(string sel);

        [AlternateSignature]
        public extern DomElement AppendTo(jQueryObject el);

        public DomElement AppendTo(DomElement de)
        {
            if (Type.GetScriptType(de) == "string")
                Element.AppendTo((string)(object) de);
            else if ((bool)Script.Literal("({0} instanceof jQuery)", de))
                Element.AppendTo((jQueryObject)(object)de);
            else
                Element.AppendTo(de.Element);
            return this;
        }

        [AlternateSignature]
        public extern DomElement PrependTo(jQueryObject el);

        public DomElement PrependTo(DomElement de)
        {
            if ((bool)Script.Literal("({0} instanceof jQuery)", de))
                Element.PrependTo((jQueryObject)(object)de);
            else
                Element.PrependTo(de.Element);
            return this;
        }

        [AlternateSignature]
        public extern DomElement InsertAfter(string sel);

        [AlternateSignature]
        public extern DomElement InsertAfter(jQueryObject el);

        public DomElement InsertAfter(DomElement de)
        {
            if (Type.GetScriptType(de) == "string")
                Element.InsertAfter((string)(object)de);
            else if ((bool)Script.Literal("({0} instanceof jQuery)", de))
                Element.InsertAfter((jQueryObject)(object)de);
            else
                Element.InsertAfter(de.Element);
            return this;
        }

        public DomElement AddClass(string classNames)
        {
            Element.AddClass(classNames);
            return this;
        }

        public DomElement Attribute(string name, object value)
        {
            Element.Attribute(name, value.ToString());
            return this;
        }

        public DomElement Html(string html)
        {
            Element.Html(html);
            return this;
        }

        public jQueryObject Select(string s)
        {
            return jQuery.Select(s, Element);
        }
    }
}
